package org.jw.testemunhopublico.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;

public class FileResourceUtil {

	private static ApplicationContext appContext = new ClassPathXmlApplicationContext(
			Constantes.APPLICATION_CONTEXT);
	
	public static Resource getResource(String filename){
		return appContext.getResource(filename);
	}
	
	public static File getFile(String filename) throws IOException{
		Resource resource = appContext.getResource(filename);
		if( resource.exists() )
			return resource.getFile();
		return null;
	}
	
	public static String getContent(String filename) throws IOException {
		File file = getFile(filename);
		if(file != null)
			return IOUtils.toString(new FileInputStream(file));
		return null;
	}

}
